package com.czy.mapper;

import com.czy.dto.UpdateAssetQuantityDto;
import com.czy.dto.AssetPageDto;
import com.czy.entity.Asset;
import com.czy.entity.BorrowRecord;
import com.czy.vo.AdminAssetPageVo;
import com.czy.vo.AssetPageVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;
import java.util.List;

@Mapper
public interface AssetMapper {
    // 查询资产总数
    @Select("SELECT SUM(stock_quantity) FROM asset")
    public Integer assetSum();

    // 用户分页查询资产表
    public List<AssetPageVo> page(AssetPageDto assetPageDto);

    //修改库存数量
    @Update("update asset set stock_quantity = #{stockQuantity} where asset_id = #{assetId}")
    public void UpdateStockQuantity(UpdateAssetQuantityDto Quantity);

    // 减少指定资产的库存数量，并返回更新后的库存信息

    /**
     * SQL 说明：
     * 1. stock_quantity - #{quantity}：用当前库存减去借出数量，结果命名为stockQuantity
     * 2. WHERE asset_id = #{assetId}：精确匹配要操作的资产
     */
    @Select("select asset_id, (stock_quantity - #{quantity}) as stockQuantity from asset WHERE asset_id = #{assetId}")
    UpdateAssetQuantityDto reduceAssetNumById(BorrowRecord borrowRecord);

    /**
     * 添加资产信息
     */
    @Insert("insert into asset ( asset_name, category_id, asset_type, " +
            "purchase_date, purchase_price, stock_quantity, location, update_time) " +
            "values (#{assetName} , #{categoryId}, #{assetType}, #{purchaseDate}, #{purchasePrice}, " +
            "#{stockQuantity}, #{location}, #{updateTime})")
    public void insertAsset(Asset asset);


    /**
     * 根据id删除资产信息
     */
    public void deleteById(ArrayList<Integer> ids);

    /**
     * 根据id查询资产信息
     */
    public AdminAssetPageVo findById(Long id);
}
